package meiyunting.shiwuzhaoling.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * 实例信息控制器
 * 用于显示当前实例的信息，便于测试负载均衡
 */
@Slf4j
@RestController
@RequestMapping("/api/instance")
public class InstanceInfoController {

    @Value("${server.port}")
    private String serverPort;
    
    @Value("${spring.cloud.consul.discovery.instance-id:${spring.application.name}:${server.port}:unknown}")
    private String instanceId;
    
    /**
     * 获取当前实例信息
     * @return 包含端口号和实例ID的信息
     */
    @GetMapping("/info")
    public Map<String, Object> getInstanceInfo() {
        Map<String, Object> info = new HashMap<>();
        info.put("port", serverPort);
        info.put("instanceId", instanceId);
        info.put("timestamp", System.currentTimeMillis());
        
        log.info("【实例信息请求】- 当前服务实例运行在端口: {}, 实例ID: {}", serverPort, instanceId);
        System.out.println("\n\n****************************************************");
        System.out.println("【实例信息-重要输出】当前服务实例运行在端口: " + serverPort + ", 实例ID: " + instanceId);
        System.out.println("****************************************************\n\n");
        
        return info;
    }
    
    /**
     * 简单的健康检查端点
     * @return 健康状态
     */
    @GetMapping("/health")
    public Map<String, Object> health() {
        Map<String, Object> health = new HashMap<>();
        health.put("status", "UP");
        health.put("port", serverPort);
        health.put("timestamp", System.currentTimeMillis());
        
        log.info("【健康检查】- 当前服务实例运行在端口: {}, 状态: UP", serverPort);
        System.out.println("【控制台输出】健康检查 - 当前服务实例运行在端口: " + serverPort);
        
        return health;
    }
} 